/********************************************************************** 
 * FILE		: CreatBarCode.java					
 *			
 * AUTHOR   : shihao	
 * 
 * DATE		: 2014-5-28
 * 									
 * REASON	: CREATE  
 *
 ***********************************************************************/

package com.common.util.barcode;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;

import org.jbarcode.JBarcode;
import org.jbarcode.encode.BarcodeEncoder;
import org.jbarcode.encode.InvalidAtributeException;
import org.jbarcode.paint.BaseLineTextPainter;
import org.jbarcode.paint.TextPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.jbarcode.util.ImageUtil;

/**********************************************************************
 * @author GA 创建条形码,一维码
 * @version 1.0
 ***********************************************************************/
public class CreateOneBarCode extends AbstractCodeUtil
{
    // 创建对象
    private static JBarcode localJBarcode = null;

    /**
     * 创建条形码,一维码
     * 
     * @param @param content 内容
     * @param @param imgPath 图片路径
     */
    public static String creatCodeParam( String content, String imgPath )
    {
        String resurt = "";
        try
        {
            localJBarcode = new JBarcode( Encoder,
                    WidthCodedPainter.getInstance(),
                    BaseLineTextPainter.getInstance() );

            resurt = saveToType( localJBarcode, imgPath, pType, content );
        } catch ( InvalidAtributeException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resurt;

    }

    /**
     * 创建条形码,一维码
     * 
     * @param @param content 内容
     * @param @param imgPath 图片路径
     * @param @param Encoder 一维码的种类,类型
     */
    public static String creatCodeParam( String content, String imgPath,
            BarcodeEncoder Encoder )
    {
        String resurt = "";
        try
        {
            localJBarcode = new JBarcode( Encoder,
                    WidthCodedPainter.getInstance(),
                    BaseLineTextPainter.getInstance() );

            resurt = saveToType( localJBarcode, imgPath, pType, content );
        } catch ( InvalidAtributeException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resurt;

    }

    /**
     * 创建条形码,一维码
     * 
     * @param paramBufferedImage
     *            创建条形码对象
     * @param imgPath
     *            图片路径
     * @param pType
     *            图片格式
     * @param content
     *            二维码内容
     * @throws InvalidAtributeException
     */
    private static String saveToType( JBarcode localJBarcode, String imgPath,
            String pType, String content ) throws InvalidAtributeException
    {
        localJBarcode.setTextPainter( CustomTextPainter.getInstance() );
        localJBarcode.setBarHeight( BARCODE_HEIGHT );

        BufferedImage localBufferedImage;

        localBufferedImage = localJBarcode.createBarcode( content );

        // saveToFile( paramBufferedImage, paramString, pType );
        try
        {
            FileOutputStream localFileOutputStream = new FileOutputStream(
                    imgPath + "." + pType );
            System.out.println( localFileOutputStream );
            ImageUtil.encodeAndWrite( localBufferedImage, pType,
                    localFileOutputStream, 96, 96 );
            localFileOutputStream.close();
        } catch ( Exception localException )
        {
            localException.printStackTrace();
        }
        return imgPath + "." + pType;
    }

    /**
     * 自定义的 TextPainter， 允许定义字体，大小等等。
     */
    private static class CustomTextPainter implements TextPainter
    {
        public static CustomTextPainter instance = new CustomTextPainter();

        public static CustomTextPainter getInstance()
        {
            return instance;
        }

        @Override
        public void paintText( BufferedImage barCodeImage, String text,
                int width )
        {
            Graphics g2d = barCodeImage.getGraphics();
            Font font = new Font( FONT_FAMILY, Font.PLAIN, FONT_SIZE * width );
            g2d.setFont( font );
            FontMetrics fm = g2d.getFontMetrics();
            int height = fm.getHeight();
            int center = (barCodeImage.getWidth() - fm.stringWidth( text )) / 2;
            // 背景的颜色
            g2d.setColor( Color.WHITE );
            g2d.fillRect( 0, 0, barCodeImage.getWidth(),
                    barCodeImage.getHeight() * 1 / 20 );
            g2d.fillRect( 0, barCodeImage.getHeight() - (height * 9 / 10),
                    barCodeImage.getWidth(), (height * 9 / 10) );
            // 字体颜色
            g2d.setColor( Color.BLACK );
            g2d.drawString( text, center, barCodeImage.getHeight()
                    - (height / 10) - 2 );
        }
    }
}